package com.bloveoacom.weichi.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.bloveoacom.pay.PayParameter;
import com.bloveoacom.util.ToastUtil;
import com.bloveoacom.weichi.R;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;


/**
 * 微信支付回调
 * Created by zhu.px on 2017/1/10.
 */

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private IWXAPI api;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        api = WXAPIFactory.createWXAPI(this, PayParameter.WX_APP_ID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq baseReq) {

    }

    @Override
    public void onResp(BaseResp baseResp) {
        int respType = baseResp.getType();
        switch (respType) {
            case ConstantsAPI.COMMAND_PAY_BY_WX:///微信支付的响应类型
                int respCode = baseResp.errCode;
                if (respCode == BaseResp.ErrCode.ERR_OK) {
                    //微信支付成功
                    setResult(RESULT_OK);
                } else {
                    if (respCode == BaseResp.ErrCode.ERR_USER_CANCEL) {
                        ToastUtil.makeShortText(this, getString(R.string.payment_cancel));
                        setResult(RESULT_CANCELED, new Intent());
                    } else {
                        ToastUtil.makeShortText(this, getString(R.string.payment_failure));
                    }
                }
                this.finish();
                break;
        }
    }
}
